/* eslint-disable */
import axios from 'axios'
import Cookie from 'js-cookie'
import localForage from 'localforage'
import router from "../router"

import {Message, MessageBox} from 'element-ui'
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // console.log("在发送请求之前做些什么");
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // console.log("对请求错误做些什么");
    // 对请求错误做些什么
    Message.error("发送请求失败！");
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // console.log("对响应数据做点什么");
    let data =response.data;
    if (data){
        switch (data.msg) {
            case "success":
            case "SUCCESS":
                return data.data;
            case "error":
            case "ERROR":
                return Promise.reject(data);
            case "LOGOUT":
            case "USER_ERROR":
                localForage.removeItem('user_info');
                localForage.removeItem('blog_user_token');
                localForage.removeItem('model');
                localForage.removeItem('userExpire');
                localForage.removeItem('modelMenus');
                Cookie.remove('url_index');
                Cookie.remove('blog_user_token');
                Cookie.remove('is_login');
                MessageBox.alert('请重新登录', '提示', {
                    type: 'error',
                    center: true
                });
                router.push({name:"login"});
                return;
            default:
                break
        }
        return data;
    }else{
        return response;
    }
}, function (error) {
    console.log(error);
    // Message.error("网络请求失败！");
    if (error.toLocaleString().search('timeout') !== -1) {
        Message({
            showClose: true,
            message: '服务器连接超时',
            type: 'error',
            duration: 0
        });
    } else if (error.toLocaleString().search('Network Error') !== -1) {
        Message('无法连接服务器', '提示', {
            type: 'error',
            center: true
        });
    } else {
        if (error.response !== undefined) {
            let data =error.response.data;
            try {
                let msg = data.msg;
                switch (msg) {
                    case "":
                        break;
                    default:
                        break;
                }
                if (data.msg === "请登录" ||data.msg === "请登录" || data.msg === "用户未登录") {
                    localForage.removeItem('user_info');
                    localForage.removeItem('blog_user_token');
                    localForage.removeItem('model');
                    localForage.removeItem('userExpire');
                    localForage.removeItem('modelMenus');
                    Cookie.remove('url_index');
                    Cookie.remove('blog_user_token');
                    Cookie.remove('is_login');
                    console.log("请登录");
                    MessageBox.alert('请重新登录', '提示', {
                        type: 'error',
                        center: true
                    });
                    window.location.href = "/";
                }

                return Promise.reject(data);
            } catch (e) {
            }
            return Promise.reject(error.response);
        }
    }
    return Promise.reject(error);
});

export default {
    post(url, params) {
        axios.defaults.headers.common['token'] = encodeURIComponent(Cookie.get('blog_user_token'));
        return axios.post(url, params, {timeout: 100000});
    },
    postFile(url, file, chunks, chunkSize, currentChunk) {
        let fileReader = new FileReader();
        fileReader.onload = function () {
            // 用FormData传输文件对象
            let fd = new FormData();
            // 设置文件上传接口的需要的参数
            fd.append('md5', file.md5);
            fd.append('chunks', chunks);
            fd.append('chunkSize', chunkSize);
            fd.append('currentChunk', currentChunk);
            // 设置上传的当前的文件块
            fd.append('file', new Blob([this.result]));
        };
        return axios.post(url, params, {timeout: 100000});
    },
    get(url, params) {
        axios.defaults.headers.common['token'] = encodeURIComponent(Cookie.get('blog_user_token'));
        return axios.get(url, {params: params}, {timeout: 100000});
    },
    delete(url, params) {
        axios.defaults.headers.common['token'] = encodeURIComponent(Cookie.get('blog_user_token'));
        return axios.delete(url, params);
    }
}
